﻿using memo.entity.PO;
using memo.entity.VO;
using memo.Services;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;

namespace memo.Controllers;

[ApiController]
[Route("[controller]")]
public class MemoController : ControllerBase{

    private readonly MemoService _memoService;
    public MemoController(MemoService memoService) {
        _memoService = memoService;
    }
    

    //增
    [HttpPost("AddMemo")]
    public Result<object> AddMemo([FromBody] Memo memo) {
        var memoId = _memoService.AddMemo(memo);
        return Result<object>.Success(memoId);
    }

    //删
    [HttpDelete("DeleteMemo/{id}")]
    public Result<object> DeleteMemo(int id) {
        var result = _memoService.DeleteMemo(id);
        return Result<object>.Success($"删除成功！删除条数为：{result}");
    }

    //改
    [HttpPut("UpdateMemo")]
    public Result<object> UpdateMemo([FromBody] Memo memo) {
        if (memo.Id == null) {
            return Result<object>.Fail("参数错误");
        }
        var result = _memoService.UpdateMemo(memo);
        return Result<object>.Success($"更新成功！更改条数为：{result}");
    }

    //查
    // 查询所有
    [HttpGet("GetMemo")]
    public Result<object> GetMemo() {
        var list = _memoService.GetAllMemos();
        return Result<object>.Success(list);
    }
    //分页查询所有
    [HttpGet("GetMemoByPage")]
    public Result<object> GetMemoByPage([FromQuery, Required] int current, [FromQuery, Required] int size) {
        var vo = _memoService.GetMemosByPage(current, size);
        return Result<object>.Success(vo);
    }
    
    // 根据id查询
    [HttpGet("GetMemoById")]
    public Result<object> GetMemoById([FromQuery, Required] int id) {
        var memo = _memoService.GetMemoById(id);
        return Result<object>.Success(memo);
    }
    
    // 查询指定用户的备忘录
    [HttpGet("GetMemoByUserId")]
    public Result<object> GetMemoByUserId([FromQuery, Required] int userId, 
                                        [FromQuery, Required] int current, 
                                        [FromQuery, Required] int size) 
    {
        var vo = _memoService.GetMemosByUserId(userId, current, size);
        return Result<object>.Success(vo);
    }
    
}

